﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using DevTrends.MvcDonutCaching;
using ECG.Common;
using ECG.DAL;
using ECG.Utility;
using ECG.WebUI.CustomHelpers;
using ECG.WebUI.Infrastructure;
using System.Configuration;

namespace ECG.WebUI.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        
        public ActionResult Index()
        {
            if (Request.Url != null) ViewBag.Canonical = Request.Url.AbsoluteUri;
            var model = FunctionHelpers.GetWebInfo();

            return View(model);
        }

        public ActionResult Header()
        {
            return PartialView();
        }
        public ActionResult HeaderNotH1Inlogo()
        {
            return PartialView();
        }
        [DonutOutputCache(CacheProfile = "OneDay")]
        public ActionResult TabHome()
        {
            return PartialView();
        }

        public ActionResult ProductHotDay(int page = 1)
        {
            var itemsPerPage = 1;
            var totalPage = 0;
            var model = ProductHotDb.GetListAllProductHotHome(page, itemsPerPage, ref totalPage) ?? new List<ProductRateByCateTopListPagingDto>();
            ViewBag.CurrentPage = page;
            ViewBag.TotalPage = totalPage;
            return PartialView(model);
        }

        public PartialViewResult ProductHotGetPaggingByAjax(int page = 1)
        {
            var itemsPerPage = 1;
            var totalPage = 0;
            var model = ProductHotDb.GetListAllProductHotHome(page, itemsPerPage, ref totalPage)[0] ?? new ProductRateByCateTopListPagingDto();
            return PartialView(model);
        }

        [DonutOutputCache(CacheProfile = "OneDay")]
        public PartialViewResult HomeProductListByCate()
        {
            var parentTemplateList = ProductCateTempMappingDb.GetByIsOnHomePage() ?? new List<VCProCateTempMappingDto>();
            var parentTemlateIdList = parentTemplateList.Aggregate(string.Empty, (current, item) => current + (item.Id + ","));
            var subTemplateList = SubCategoryTemplateDb.GetAllByParentTemplateId(parentTemlateIdList) ?? new List<SubCategoryTemplateDto>();

            var productList = new Dictionary<string, List<ProductRateByCateTopListPagingDto>>();
            foreach (var item in parentTemplateList)
            {
                var pList = ProductDb.GetByBreadCrumbCateId(item.ProductCateId) ?? new List<ProductRateByCateTopListPagingDto>();
                productList.Add(item.ProductCateId, pList);
            }

            ViewBag.ParentTemplateList = parentTemplateList;
            ViewBag.SubTemplateList = subTemplateList;
            ViewBag.ProductList = productList;
            ViewBag.BannerByCateList = BannerDb.GetAllBanner() ?? new List<ViewBannerClientDto>();

            return PartialView();
        }

        [DonutOutputCache(CacheProfile = "OneDay")]
        public ActionResult HomeProductListByCate2()
        {
            var cateListAll = ProductCategoryDb.GetAll() ?? new List<ProductCategoryDto>();
            var cateListShowHome = (from c in cateListAll where c.ParentId == "0" && c.ShowHome orderby c.OrderOnHome ascending select c).ToList();//cateListAll.Where(x => x.ParentId == "0" && x.ShowHome).OrderBy(x => x.OrderOnHome).ToList();

            ViewBag.CateList = cateListAll;
            ViewBag.CateListShowHome = cateListShowHome;
            var objList = new Dictionary<int, object>();
            if (cateListShowHome.Count > 0)
            {
                var count = 0;
                var totalPage = 0;
                foreach (var item in cateListShowHome)
                {
                    count++;
                    //var pList = ProductDb.GetTopProductByCateIdRate(item.Id) ?? new List<ProductRateByCateTopListPagingDto>();
                    var pList = ProductDb.GetAllProductByCategoryId(1, 5, ref totalPage, item.Id) ?? new List<ProductRateByCateTopListPagingDto>();
                    objList.Add(count, pList);
                }
            }
            ViewBag.ObjectList = objList;
            return PartialView();
        }

        public JsonResult SearchAutoComplete(string query = "")
        {
            var model = ProductDb.GetProductNameListByKeyword(query) ?? new List<ProductNameListDto>();
            var strArr = model.Select(item => item.Name).ToList();
            var data = new
                {
                    query,
                    suggestions = strArr
                };
            return Json(data, JsonRequestBehavior.AllowGet);
        }

        [DonutOutputCache(CacheProfile = "OneDay")]
        public ActionResult ProviderByCate(string category,string cateName)
        {
            var data = ProviderDb.GetAllProviderByKeyWordAdmin(category) ?? new List<ProviderListCateHomeDto>();
            ViewBag.ParentId = category;
            ViewBag.CateName = cateName;
            return PartialView(data);
        }

        #region Footer

        [DonutOutputCache(CacheProfile = "OneDay")]
        public ActionResult Footer()
        {
            var modelFooter = FunctionHelpers.GetWebInfo();
            return PartialView(modelFooter);
        }

        [DonutOutputCache(CacheProfile = "OneDay")]
        public ActionResult FooterBottomOne()
        {
            return PartialView();
        }

        [DonutOutputCache(CacheProfile = "OneDay")]
        public ActionResult HomeFooterText()
        {
            return PartialView();
        }

        #endregion

        #region Menu


        //dang su dung
        public ActionResult Menu()
        {
            var model = CacheLayer.Get<List<ProductCategoryDto>>(ConstVariableList.MemCacheMenu);
            if (model == null)
            {
                model = ProductCategoryDb.GetAll() ?? new List<ProductCategoryDto>();
                CacheLayer.Add(model, ConstVariableList.MemCacheMenu);
            }
            ViewBag.Menu = model;
            //banner
            ViewBag.Banner = ProductCategoryBannerDb.GetAllBannerByWidth(300, 0) ?? new List<ProductCategoryBannerWidthDto>();
            //ViewBag.Provider = ProviderDb.GetAll();
            return PartialView();
        }

        [DonutOutputCache(CacheProfile = "OneDay")]
        public PartialViewResult BoxProductMenuHome(string category)
        {
            var data = ProductDb.GetByMenuHome(category) ?? new List<ProductMenuHomeDto>();
            return PartialView(data);
        }

        [DonutOutputCache(CacheProfile = "OneDay")]
        public PartialViewResult GetCategoryBanner(string category)
        {
            var data = ProductCategoryBannerDb.GetListBannerByCategory(category) ?? new List<ProductCategoryBannerHomeDto>();
            return PartialView(data);
        }

        //--Hien thi ten nha cung cap theo danh muc trong menu
        public PartialViewResult GetProviderForMenu(string categoryId)
        {
            var data = ProviderDb.GetAllProviderByKeyWordAdmin(categoryId) ?? new List<ProviderListCateHomeDto>();
            return PartialView(data);
        }


        ////hien thi anh dai dien cho danh muc khi di chuot vao menu
        //[DonutOutputCache(CacheProfile = "OneDay")]
        //public PartialViewResult AdvByCategoryMenuHome(string categoryId, int w, int o)
        //{
        //    var bList = ProductCategoryBannerDb.GetListBannerByWidth(categoryId, w, o) ?? new List<ProductCategoryBannerHomeDto>();
        //    return PartialView(bList);
        //}
        #endregion
        
    }
}
